<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-7-15
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class UserHasRolesDB  extends DBReader{
    private $sql_param = 'role_id, user_id';

    //添加用户拥有的角色信息
    public function addUserHasRoles($user_id,$role_id){

        $sql = "insert into user_has_roles(role_id,user_id) values(:role_id,:user_id)";

        $bindarr = array(
            'role_id'=>$role_id,
            'user_id'=>$user_id,
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除 role_id 对应的信息
    public function delByRoleId($role_id){
        $bindarr = array(
            'role_id'=>$role_id,
        );

        $sql = "delete from user_has_roles where role_id = :role_id";
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    //删除 user_id 对应的信息
    public function delByUserId($user_id){
        $bindarr = array(
            'user_id'=>$user_id,
        );

        $sql = "delete from user_has_roles where user_id = :user_id";
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    /**
     * @tudo 查询用户拥有的角色
     * @param $user_id
     * @return array|mixed
     */
    public function getUserHasRoles($user_id){

        $sql = "select r.role_id,r.role_name from user_has_roles u left join roles r on u.role_id = r.role_id where user_id = :user_id";

        $bindarr = array(
            'user_id'=>$user_id,
        );

        return $this->db()->getAll($sql,$bindarr);
    }
}